package com.kayb.patterns.creational;

/**
 * 建造者模式，用于创建类的复合对象，通俗来说就是多个属性。
 * vs 工厂模式：工厂模式关注单个类的创建，而建造者模式关注的是类的多个属性创建。
 * @author huangkaibin
 * @date 2018-08-28
 **/
public class Builder {

    private String name;
    private int age;

    public static class InnerBuilder {
        private String name;
        private int age;

        public InnerBuilder setName(String name) {
            this.name = name;
            return this;
        }

        public InnerBuilder setAge(int age) {
            this.age = age;
            return this;
        }

        public Builder build() {
            Builder builder = new Builder();
            builder.age = this.age;
            builder.name = this.name;
            return builder;
        }

        public static InnerBuilder newBuilder() {
            return new InnerBuilder();
        }
    }

    public static void main(String[] args) {
        Builder builder = InnerBuilder.newBuilder().setAge(1).setName("Alex").build();
        System.out.println(builder.name);
    }

}
